package ecoll.client.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;

import ecoll.client.Client;
import ecoll.client.centers.NewsCenter;

public class QuizUserPanel extends HorizontalPanel {

	public QuizUserPanel(final String roomName) {
		final TextBox answerBox = new TextBox();

		answerBox.addKeyDownHandler(new KeyDownHandler() {
			@Override
			public void onKeyDown(KeyDownEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					Client.QUIZ_SERVICE.userActivity(roomName,
							answerBox.getText(), new AsyncCallback<Boolean>() {

								@Override
								public void onFailure(Throwable e) {
									Window.alert(e.getMessage());
								}

								@Override
								public void onSuccess(Boolean result) {
									// TODO Auto-generated method stub
								}
							});
					answerBox.setText("");
				}

			}

		});
		add(answerBox);
		answerBox.setWidth("100%");

		Button leaveButton = new Button("Leave");
		leaveButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				new NewsCenter().execute();
			}
		});
		leaveButton.addStyleName("button");
		add(leaveButton);
		setCellVerticalAlignment(leaveButton, HasVerticalAlignment.ALIGN_MIDDLE);
	}
}
